package com.ruoyi.web.controller.hz;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HzComment;
import com.ruoyi.system.service.IHzCommentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评论实体Controller
 *
 * @author ruoyi
 * @date 2021-08-08
 */
@Controller
@RequestMapping("/comment")
public class HzCommentController extends BaseController {
    private String prefix = "comment";

    @Autowired
    private IHzCommentService hzCommentService;

    @GetMapping()
    public String comment() {
        return prefix + "/comment";
    }

    /**
     * 查询评论实体列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HzComment hzComment) {
        startPage();
        List<HzComment> list = hzCommentService.selectHzCommentList(hzComment);
        return getDataTable(list);
    }

    /**
     * 导出评论实体列表
     */
    @Log(title = "评论实体", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HzComment hzComment) {
        List<HzComment> list = hzCommentService.selectHzCommentList(hzComment);
        ExcelUtil<HzComment> util = new ExcelUtil<HzComment>(HzComment.class);
        return util.exportExcel(list, "评论实体数据");
    }

    /**
     * 新增评论实体
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存评论实体
     */
    @Log(title = "评论实体", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HzComment hzComment) {
        SysUser user = ShiroUtils.getSysUser();
        if (user != null) {
            HzComment comment = new HzComment(user.getUserId(), hzComment.getUploadInfoId(), hzComment.getCommentMsg());
            return toAjax(hzCommentService.insertHzComment(comment));
        } else {
            return AjaxResult.error("请登录");
        }
    }

    /**
     * 修改评论实体
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        HzComment hzComment = hzCommentService.selectHzCommentById(id);
        mmap.put("hzComment", hzComment);
        return prefix + "/edit";
    }

    /**
     * 修改保存评论实体
     */
    @RequiresPermissions("system:comment:edit")
    @Log(title = "评论实体", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HzComment hzComment) {
        return toAjax(hzCommentService.updateHzComment(hzComment));
    }

    /**
     * 删除评论实体
     */
    @RequiresPermissions("system:comment:remove")
    @Log(title = "评论实体", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(hzCommentService.deleteHzCommentByIds(ids));
    }
}
